var prefix = jsctx + "/operatorPowerTimePeriod";

var operatorPowerTimePeriod = (function () {
    function init() {
        getResellerElecStation()
    };

    $('#exampleTable').on('load-success.bs.table', function (e, data) {
        if (data.total && !data.rows.length) {
            $('#exampleTable').bootstrapTable('selectPage').bootstrapTable('refresh');
        }
    });

    function reLoad() {
        var startTime = $("#startTime").val();
        if (startTime == '' || startTime == null) {
            parent.layer.alert("请选择开始时间");
            return;
        }
        listEchars();
    }

    //获取分销商站点
    function getResellerElecStation() {
        $.ajax({
            "url": jsctx + "/elecStation/listElecStationData",
            "type": "GET",
            "data": {},
            "success": function (data) {
                console.log(data);
                var str = ''
                for (var i = 0; i < data.length; i++) {
                    if (i == 0) {
                        str = "<option value=" + data[i].elecStationId + " selected>" + data[i].stationName + "</option>";
                    } else {
                        str += "<option value=" + data[i].elecStationId + " >" + data[i].stationName + "</option>";
                    }

                }
                //console.log(str)
                $("#resellerElecStation").append(str);
                $('#resellerElecStation').searchableSelect();
            }, complete: function () {
                listEchars()
            }
        })
    }

    //获取数据
    function listEchars() {
        var myChart = echarts.init(document.getElementById('main'));
        myChart.showLoading();
        $.ajax({
            "url": prefix + "/listOperatorPowerTimePeriod",
            "method": "GET",
            async: false,
            "data": {
                startTime: $("#startTime").val().trim(),
                endTime: $("#endTime").val().trim(),
                // bizType: $("#bizType").val().trim(),
                elecStationId: $("#resellerElecStation").val(),
                userType: $("#userType").val().trim(),
                code: $("#code").val().trim()
            },
            "success": function (data) {
                myChart.hideLoading();
                d(data, myChart);
            }
        })
    }


    function d(data, myChart) {
        var x = [];
        var y = [];
        for (var i = 0; i < data.length; i++) {
            x.push(data[i].times)
            y.push(data[i].totalPower)
        }
        // 基于准备好的dom，初始化echarts实例
        option = {
            color: ['#3398DB'],
            tooltip: {
                trigger: 'axis',
                axisPointer: {            // 坐标轴指示器，坐标轴触发有效
                    type: 'shadow'        // 默认为直线，可选为：'line' | 'shadow'
                }
            },
            grid: {
                left: '5%',
                right: '0%',
                bottom: '0%',
                containLabel: true
            },
            xAxis: [
                {
                    type: 'category',
                    data: x,
                    axisTick: {
                        alignWithLabel: true
                    }
                }
            ],
            yAxis: [
                {
                    name: '电量',
                    type: 'value'
                }
            ],
            series: [
                {
                    type: 'bar', barWidth: '30%',
                    data: y
                }
            ]
        };
        myChart.setOption(option);

    };

    function exportOperatorPowerTimePeriod() {
        $.fileDownload(prefix + "/exportOperatorPowerTimePeriod", { // 后台请求链接
            httpMethod: "get",　　　　　　　　// 请求方式
            contentType: 'application/json',　 // 请求头
            data: {　　　　　　　　　　　　　　　// 参数，左边为后台接收的参数名，右边为前端获取的值，
                startTime: $("#startTime").val().trim(),
                endTime: $("#endTime").val().trim(),
                endTime: $("#endTime").val().trim(),
                elecStationId: $("#resellerElecStation").val()
            },
            prepareCallback: function (url) {
                layer.msg("文件下载中...", {time: -1})
            },
            abortCallback: function (url) {
                // 异常终止
                layer.msg("文件下载异常！！");
            },
            successCallback: function (url) {
                layer.msg("文件下载成功！！", {time: 1500});
            },
            failCallback: function (data) {
                var str = "{" + data.substring(data.indexOf("{") + 1, data.indexOf("}")) + "}";
                layer.msg(JSON.parse(str).message);
            }
        })

    }
    // $("#resellerElecStation").change(function () {
    //     alert(2323)
    //     listEchars()
    // })

    return {
        init: init,
        reLoad: reLoad,
        exportOperatorPowerTimePeriod: exportOperatorPowerTimePeriod,
        listEchars:listEchars
    };

})();

